package com.example.write_exam2.service;

import com.example.write_exam2.entity.Fruit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 笔试题作答
 * @author 李荣华
 * @date 2025--07--15
 */
@Service
@Slf4j
public class AnswerService {

    /**
     * 满减
     */
    public static final BigDecimal FULL_REDUCTION = new BigDecimal(100);

    /**
     * 商品打折标志
     */
    public static final String FRUIT_DISCOUNT = "1";

    /**
     * 满减打折标志
     */
    public static final String FULL_DISCOUNT = "1";

//    /**
//     * 购买苹果和草莓并计算总价
//     * @param applesWeight 苹果重量
//     * @param strawWeight 草莓重量
//     * @return 总价
//     */
//    public BigDecimal buyFruit1(int applesWeight, int strawWeight){
//        //创建苹果和草莓对象，正常是从数据库取已有的对应水果数据
//        //苹果
//        Fruit apples = new Fruit();
//        apples.setName("苹果").setUnitPrice(new BigDecimal(8)).setIsDiscount("0");
//        //草莓
//        Fruit strawberry = new Fruit();
//        strawberry.setName("草莓").setUnitPrice(new BigDecimal(13)).setIsDiscount("0");
//        //苹果花费总额 = 苹果单价*苹果重量
//        BigDecimal applesTotal = apples.getUnitPrice().multiply(new BigDecimal(applesWeight));
//        //判断苹果是否是打折的商品，如果是苹果所要花费的总额*折扣就是顾客实际花费买苹果的金额
//        if (FRUIT_DISCOUNT.equals(apples.getIsDiscount())) {
//            applesTotal = applesTotal.multiply(apples.getDiscount());
//        }
//        log.info("顾客A购买苹果所花费：{}", applesTotal);
//        //草莓花费总额 = 草莓单价*草莓重量
//        BigDecimal strawTotal = strawberry.getUnitPrice().multiply(new BigDecimal(strawWeight));
//        //判断草莓是否打折商品，如果是，草莓花费的总额*折扣就是顾客买草莓所花费的金额
//        if (FRUIT_DISCOUNT.equals(strawberry.getIsDiscount())) {
//            strawTotal = strawTotal.multiply(strawberry.getDiscount());
//        }
//        log.info("顾客A购买草莓所花费：{}", strawTotal);
//        //总价 = 苹果总价 + 草莓总价
//        BigDecimal total = applesTotal.add(strawTotal);
//        log.info("顾客A购买水果所花费总额：{}", total);
//        return total;
//    }

    /**
     * 购买苹果和草莓以及芒果并计算总价
     * @param fullDiscout 是否有满减活动
     * @param list 水果信息
     * @return 总价
     */
    public BigDecimal buyFruit(String fullDiscout, List<Fruit> list){
        BigDecimal total = new BigDecimal(0);
        for (Fruit fruit : list) {
            BigDecimal fruitTotal = fruit.getUnitPrice().multiply(new BigDecimal(fruit.getWeight()));
            if (FRUIT_DISCOUNT.equals(fruit.getIsDiscount())) {
                fruitTotal = fruitTotal.multiply(fruit.getDiscount());
            }
            log.info("水果" + fruit.getName() + "花费" + fruitTotal + "元");
            total = total.add(fruitTotal);
        }
        log.info("顾客购买水果所花费总额：{}", total);
        //判断超时是否有满减活动
        if (FULL_DISCOUNT.equals(fullDiscout)) {
            //判断是否符合满减,满100减10
            if (total.compareTo(FULL_REDUCTION) >= 0) {
                total = total.subtract(new BigDecimal(10));
            }

        }
        return total;
    }

}
